package com.timu;

import java.util.Calendar;
import java.util.Date;

public class Vip {
    /**
     * 体验套餐，时长3天
     */
    public static final int EXPERIENCE = 1;
    /**
     * 体验套餐，时长7天
     */
    public static final int WEEK = 2;
    /**
     * 体验套餐，时长1个月
     */
    public static final int MONTH = 3;
    /**
     * 体验套餐，时长3个月
     */
    public static final int QUARTER = 4;
    /**
     * 体验套餐，时长1年
     */
    public static final int YEAR = 5;

    private Date start;

    private Date end;

    public Vip(int level) {
        init(level);
    }

    private void init(int level) {
        Calendar calendar = Calendar.getInstance();
        start = calendar.getTime();
        addDateByLevel(calendar, level);
        end = calendar.getTime();
    }

    /**
     * 在指定日期的基础上增加一部分时间
     * @param calendar 起始日期
     * @param level 套餐级别
     */
    private void addDateByLevel(Calendar calendar, int level) {
        switch (level) {
            case EXPERIENCE:
                calendar.add(Calendar.DATE, 3);
                break;
            case WEEK:
                calendar.add(Calendar.DATE, 7);
                break;
            case MONTH:
                calendar.add(Calendar.MONTH, 1);
                break;
            case QUARTER:
                calendar.add(Calendar.MONTH, 3);
                break;
            case YEAR:
                calendar.add(Calendar.YEAR, 1);
                break;
            default:
                break;
        }
    }

    /**
     * 续费
     * @param level 套餐级别
     */
    public void renew(int level) {
        // 当前时间肯定是比开通时间要晚
        Calendar calendar = Calendar.getInstance();
        // 不确定是否比过期时间早或晚，判断过期没有
        Date now = calendar.getTime();
        if (now.after(end)) {
            // 过期了重新生成
            init(level);
        } else {
            // 还没有过期，开始时间不变，结束时间叠加
            // 获取end的Calendar对象
            calendar.setTime(end);
            addDateByLevel(calendar, level);
            end = calendar.getTime();
        }
    }

    public Date getStart() {
        return start;
    }

    public Date getEnd() {
        return end;
    }
}
